package ee.ttu.agent.smartHouse.gui;

import jade.gui.GuiEvent;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ee.ttu.agent.smartHouse.agent.SupervisorAgent;
import ee.ttu.agent.smartHouse.util.CustomEventTypes;

public class SupervisorAgentGui extends JFrame {
	private static final long serialVersionUID = -3112825135396903541L;

	private JButton attemptInfoButton;
	private JButton startOnlineInteraction;
	private JButton denyUnlockButton;
	private JButton onHoldButton;
	
	private JLabel infoLabel;
	private JCheckBoxMenuItem checkBoxMenuItem;

	private SupervisorAgent myAgent;

	public SupervisorAgentGui(SupervisorAgent supervisorAgent) {
		this.setSize(300, 300);
		this.setTitle("Supervisor Application Panel");
		this.setLocation(200, 400);
		
		Container content = this.getContentPane();
		content.setBackground(Color.white);
		content.setLayout(new FlowLayout());

		JPanel p = new JPanel();
		p.setLayout(new GridLayout(6,1));
		
		infoLabel = new JLabel();
		p.add(infoLabel);
		
		attemptInfoButton = new JButton("Approve Door Unlock");
		p.add(attemptInfoButton);
		
		checkBoxMenuItem = new JCheckBoxMenuItem("Approve Permanently");
		p.add(checkBoxMenuItem);
		
		onHoldButton = new JButton("Put On Hold");
		
		denyUnlockButton = new JButton("Deny Door Unlock");
		p.add(denyUnlockButton);
		
		startOnlineInteraction = new JButton("Start Online Interaction");
		p.add(startOnlineInteraction);
		
		content.add(p);
		
		attemptInfoButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				GuiEvent ge = new GuiEvent(this,
						CustomEventTypes.SUPERVISOR_APPROVE);
				myAgent.postGuiEvent(ge);
			}
		});
		
		denyUnlockButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				GuiEvent ge = new GuiEvent(this,
						CustomEventTypes.SUPERVISOR_DECLINE);
				myAgent.postGuiEvent(ge);
			}
		});
		
		onHoldButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				GuiEvent ge = new GuiEvent(this,
						CustomEventTypes.SUPERVISOR_ONHOLD);
				myAgent.postGuiEvent(ge);
			}
		});

		startOnlineInteraction.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				GuiEvent ge = new GuiEvent(this,
						CustomEventTypes.SUPERVISOR_OI_START);
				myAgent.postGuiEvent(ge);
			}
		});
	

		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(WindowEvent winEvt) {
				GuiEvent ge = new GuiEvent(this, CustomEventTypes.KILL_AGENT);
				myAgent.postGuiEvent(ge);
			}
		});

		myAgent = supervisorAgent;
	}

	public boolean isPerm(){
		return checkBoxMenuItem.isSelected();
	}
	
	public void showMsg(String content) {
		infoLabel.setText(content);
	}


	public void actionPerformed(ActionEvent e) {

	}
}
